package com.tao.auth.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Component;

/**
 * <p>
 *
 * </p>
 *
 * @author taohongrun
 * @since 2024/11/23
 */
@Slf4j
@Component
public class DaoAuthenticationProviderCustom extends DaoAuthenticationProvider {

    @Autowired
    public void setUserDetailsService(UserDetailsService userDetailsService) {
        super.setUserDetailsService(userDetailsService);
    }


    //屏蔽密码对比
    protected void additionalAuthenticationChecks(UserDetails userDetails, UsernamePasswordAuthenticationToken authentication) throws AuthenticationException {


    }

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        // 调用父类的 authenticate 方法进行认证
        Authentication authResult = super.authenticate(authentication);

        // 将 authentication 对象放入 SecurityContextHolder 中
        SecurityContextHolder.getContext().setAuthentication(authResult);

        return authResult;
    }
}
